#include "Sprite.h"
#include "Renderer.h"
//----------------------------------------------------------------
ns::Sprite::Sprite()
	:
	Entity2D()
{
	// MIO
	/*
	float size = 0.5f;
	float sizeUV = 1.0f;

	m_TextureVertex[0].x = size;
	m_TextureVertex[0].y = size;
	m_TextureVertex[0].z = 0;
	m_TextureVertex[0].u = 0;
	m_TextureVertex[0].v = 0;
	
	m_TextureVertex[1].x = -size;
	m_TextureVertex[1].y = size;
	m_TextureVertex[1].z = 0;
	m_TextureVertex[1].u = sizeUV;
	m_TextureVertex[1].v = 0;
	
	m_TextureVertex[2].x = size;
	m_TextureVertex[2].y = -size;
	m_TextureVertex[2].z = 0;
	m_TextureVertex[2].u = 0;
	m_TextureVertex[2].v = sizeUV;

	m_TextureVertex[3].x = -size;
	m_TextureVertex[3].y = -size;
	m_TextureVertex[3].z = 0;
	m_TextureVertex[3].u = sizeUV;
	m_TextureVertex[3].v = sizeUV;*/


//DANI

	// set the vertex values
	TextureVertex* pkV = &(m_TextureVertex[0]);
	pkV->x = -0.5f;	pkV->y = -0.5f;	pkV->z = 1.0f;
	pkV->u = 0.0f;	pkV->v = 1.0f;
	
	pkV = &(m_TextureVertex[1]);
	pkV->x = -0.5f;	pkV->y = 0.5f;	pkV->z = 1.0f;
	pkV->u = 0.0f;	pkV->v = 0.0f;

	pkV = &(m_TextureVertex[2]);
	pkV->x = 0.5f;	pkV->y = -0.5f;	pkV->z = 1.0f;
	pkV->u = 1.0f;	pkV->v = 1.0f;

	pkV = &(m_TextureVertex[3]);
	pkV->x = 0.5f;	pkV->y = 0.5f;	pkV->z = 1.0f;
	pkV->u = 1.0f;	pkV->v = 0.0f;
}
//----------------------------------------------------------------
ns::Sprite::~Sprite()
{

}
//----------------------------------------------------------------
void ns::Sprite::Draw(Renderer* renderer)
{
	Entity2D::Draw(renderer);

	if(renderer->BindTexture(m_pTexture))
		renderer->Draw(m_TextureVertex, PrimitiveType::TriangleStrip , 4);
	else
		assert(0);
}
//----------------------------------------------------------------
void ns::Sprite::Clone(Sprite* sprite)
{
	//DANI
	// check for clone to self
	if(sprite == this)
		return;

	// increment smart pointers references
	sprite->m_pTexture = m_pTexture;

	// copy vertices
	for(unsigned int i=0; i<4; i++)
		sprite->m_TextureVertex[i] = m_TextureVertex[i];
}
//----------------------------------------------------------------
void ns::Sprite::setTextureArea(unsigned int uiPosX, unsigned int uiPosY, unsigned int uiWidth, unsigned int uiHeight)
{
	//DANI
	assert(m_pTexture);

	float fTexWidth = static_cast<float>(m_pTexture->getWidth());
	float fTexHeight = static_cast<float>(m_pTexture->getHeight());
	
	// calculate the texture coordinates
	float fU1 = uiPosX / fTexWidth;
	float fU2 = (uiPosX + uiWidth) / fTexWidth;

	float fV1 = uiPosY / fTexHeight;
	float fV2 = (uiPosY + uiHeight) / fTexHeight;

	// update the vertices
	
	TextureVertex* pkV = &(m_TextureVertex[0]);
	pkV->u = fU1;	pkV->v = fV2;
	
	pkV = &(m_TextureVertex[1]);
	pkV->u = fU1;	pkV->v = fV1;

	pkV = &(m_TextureVertex[2]);
	pkV->u = fU2;	pkV->v = fV2;

	pkV = &(m_TextureVertex[3]);
	pkV->u = fU2;	pkV->v = fV1;
}
